package com.suneee.logistic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.enterprise.entity.Enterprise;
import com.suneee.logistic.dao.EnterpriseLogisticDao;
import com.suneee.logistic.dao.LogisticDao;
import com.suneee.logistic.entity.EnterpriseLogistics;
import com.suneee.logistic.entity.Logistic;
import com.suneee.logistic.entity.LogisticsDto;
import com.suneee.logistic.service.EnterpriseLogisticService;
import com.suneee.resources.ProductError;

@Service("api-enterpriseLogistic")
public class EnterpriseLogisticServiceImpl implements EnterpriseLogisticService {

	@Autowired
	private EnterpriseLogisticDao enterpriseLogisticDao;
	@Autowired
	private LogisticDao logisticDao;
	
	@Override
	@Transactional
	public void add(List<EnterpriseLogistics> enLogisticsList) {
		Assert.notNull(enLogisticsList, "物流公司对象不能为空");
		Enterprise enterprise = ContextHelper.getCurrentLoginInfo().getEnterprise();
		enterpriseLogisticDao.deleteWhere(" enterprise_code=?", enterprise.getEnterpriseCode());
		
		for (EnterpriseLogistics enterpriseLogistics : enLogisticsList) {
			if (!logisticDao.existsId(enterpriseLogistics.getCompanyCode())) {
				throw ErrorUtils.wrap(ProductError.IOGISTIC_DUPLICATE);
			}
			
//			if (enterpriseLogisticDao.existsCompanyCode(enterpriseLogistics.getCompanyCode(),enterprise.getEnterpriseCode())) {
//				throw ErrorUtils.wrap(ProductError.ENTERPRISE_IOGISTIC_DUPLICATE);
//			}
			enterpriseLogistics.setEnterpriseCode(enterprise.getEnterpriseCode());
			enterpriseLogistics.setDefaults(enterpriseLogistics.isDefaults());
			Logistic logistic = logisticDao.select(enterpriseLogistics.getCompanyCode());
			enterpriseLogistics.setCompanyName(logistic.getCompanyName());
			enterpriseLogisticDao.insert(enterpriseLogistics);
		}
	}

	@Override
	@Transactional
	public void delete(int configId) {
		Assert.notNull(configId, "配置id");
		enterpriseLogisticDao.delete(configId);
		
	}

	@Override
	public void isDefault(int logisticId) {
		Assert.notNull(logisticId, "配置id");
		
		Enterprise enterprise = ContextHelper.getCurrentLoginInfo().getEnterprise();
		List<EnterpriseLogistics> list = enterpriseLogisticDao.listLogistic(enterprise.getEnterpriseCode());
		//先把企业下所有的快递都不默认 然后再设置 新的快递公司成默认的。 
		for (EnterpriseLogistics enterpriseLogistics : list) {
			enterpriseLogisticDao.isDefault(false, enterpriseLogistics.getLogisticId());
		}
		enterpriseLogisticDao.isDefault(true, logisticId);
	}

	@Override
	public void isDefaultNo(int configId) {
		Assert.notNull(configId, "配置id");
		enterpriseLogisticDao.isDefault(false, configId);
	}

	@Override
	public List<EnterpriseLogistics> listEnterpriseLogistics() {
		Enterprise enterprise = ContextHelper.getCurrentLoginInfo().getEnterprise();
		return enterpriseLogisticDao.listLogistic(enterprise.getEnterpriseCode());
	}

	@Override
	public Page<EnterpriseLogistics> pageEnLogistics(LogisticsDto dto) {
		Enterprise enterprise = ContextHelper.getCurrentLoginInfo().getEnterprise();
		dto.setEnterpriseCode(enterprise.getEnterpriseCode());
		return enterpriseLogisticDao.pageEnLogistics(dto);
	}

}
